package cn.airnan.dg.entity;

import cn.airnan.dg.configs.Global;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Room {

    private static final Logger log = LoggerFactory.getLogger(Room.class);
    /**
     * 房间ID
     */
    private String id;

    /**
     * 房主ID
     */
    private String master;

    /**
     * 玩家列表(最多五人)
     */
    private List<Player> players = new ArrayList<>(5);

    /**
     * 画板
     */
    private Board board;

    /**
     * 题目
     */
    private Topic topic;

    /**
     * 游戏开始状态
     */
    private boolean isBegin = false;

    /**
     * 倒计时
     */
    private Integer countDown = 0;

    /**
     * 添加玩家
     *
     * @param player 要添加的玩家
     */
    public void addPlayer(Player player) {
        if (players.size() < 5) {
            players.add(player);
        } else {
            System.out.println("房间已满，无法添加更多玩家。");
        }
    }

    /**
     * 根据玩家id移除房间内的玩家
     *
     * @param playerId 要移除的玩家id
     */
    public void removePlayerById(String playerId) {
        Player player = new Player();
        player.setId(playerId);
        players.remove(player);
    }

    /**
     * 广播消息给房间内所有玩家
     *
     * @param type     消息类型
     * @param message  要广播的消息
     * @param playerId 发送者的ID（用于排除发送者）
     */
    public void sendMessage(String type, String playerId, String message) {
        if (type.startsWith(Global.MSG_TYPE_ROOM_DRAW)) {
            log.info("【绘画房间消息】{}: {}", id, message);
            for (Player player : players) {
                // 向除自己之外的玩家发送消息
                if (!player.getId().equals(playerId)) {
                    player.sendMessage(type, message);
                }
            }
        } else {
            log.info("【全员房间消息】{}: {}", id, message);
            for (Player player : players) {
                // 向房间内所有玩家发送
                player.sendMessage(type, message);
            }
        }

    }

}
